<template>
  <div class="login-wrap">
    <!-- 系统公告 -->
    <notice/>
    <el-form ref="loginForm" :model="loginForm" label-width="0px" class="login-form">
      <h3 class="title">JEEDCP管理系统</h3>
      <el-form-item :rules="$rulesFilter({required:true})" prop="username">
        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="用户名">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item :rules="$rulesFilter({required:true})" prop="password">
        <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item :rules="$rulesFilter({required:true,validator:validCaptcha})" prop="captcha" class="code-wape">
        <el-input v-model="loginForm.captcha" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
        </el-input>
        <div class="login-code">
          <graphic-code :content-width="115" @success="generateCode"/>
        </div>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="medium" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
      <div class="link-a-wape">
        <svg-icon icon-class="weixin" class-name="link-svg-weixin"/> <a :href="weixinLoginUrl">微信登录</a>
      </div>
      <div class="rememberme-wape clearfix">
        <!-- <el-checkbox v-model="isRememberMe">记住我</el-checkbox> -->
        <router-link to="/signup" class="singup">注册申请</router-link>
        <router-link to="/forget/password" class="forget-pwd">忘记密码？</router-link>
      </div>
    </el-form>
    <!-- <div class="ms-focus-on-wechat">
      <div style="color:#fff;margin-bottom:10px;font-size:12px;">请用微信扫描关注服务号</div>
      <img src="static/images/login/qrcode_for_gh_c43ab06d6a64_344.jpg" width="120" height="120">
      <div style="color:#fff;margin-bottom:10px;font-size:12px;margin-top:20px;">微信小程序入口</div>
      <img src="static/images/login/xiaochengxu.png" width="120" height="120">
    </div> -->
    <div class="ms-tips">温馨提示：平台支持Google Chrome版本和360浏览器（采用极速模式），建议推荐<a style="color:#c2c3c3;font-weight: bold;" href="https://www.google.cn/chrome/" target="_blank">谷歌浏览器</a>。</div>
  </div>
</template>

<script>
import GraphicCode from '@/components/GraphicCode'
import Notice from './notice'
const Base64 = require('js-base64').Base64
export default {
  name: 'Login',
  components: {
    Notice,
    GraphicCode
  },
  data() {
    return {
      loading: false,
      isRememberMe: true,
      captchaPath: '',
      loginForm: {
        username: 'admin',
        password: '123456',
        captcha: ''
      },
      verifiedCode: '',
      baseAPI: process.env.BASE_API,
      weixinLoginUrl: process.env.WEIXIN_LOGIN_URL
    }
  },
  computed: {
    baseURL() {
      // return this.baseAPI.replace('/whjk', '')
      return this.baseAPI
    }
  },
  created() {
    // this.getCaptcha()
  },
  mounted: function() {
    this.checkBrowser() // 检测浏览器
  },
  methods: {
    checkBrowser() {
      var ua = navigator.userAgent.toLocaleLowerCase()
      if (ua.match(/chrome/) == null && ua.match(/firefox/) == null) {
        // 既不是chrome内核，也不是火狐浏览器
        this.$alert(
          `系统检测到您的浏览器可能不兼容本系统，建议您下载以下浏览器：<br /><br /><a href="${
            this.baseURL
          }/welcome/tools/49.0.2623.112_chrome_installer.exe" target="_blank">Chrome下载</a>&nbsp;或者&nbsp;<a href="${
            this.baseURL
          }/welcome/tools/360cse_9.5.0.138.exe" target="_blank">360极速浏览器下载</a>`,
          '温馨提示',
          {
            dangerouslyUseHTMLString: true
          }
        )
      }
    },
    validCaptcha(rule, value, callback) {
      if (this.loginForm.captcha.toLowerCase() !== this.verifiedCode) {
        return callback(new Error('您输入的验证码不正确!'))
      } else {
        return callback()
      }
    },
    generateCode(value) {
      this.verifiedCode = value.toLowerCase()
    },
    handleLogin() {
      var _this = this
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          const postData = {}
          postData.username = Base64.encode(
            Base64.encode(Base64.encode(_this.loginForm.username))
          )
          postData.password = Base64.encode(
            Base64.encode(Base64.encode(_this.loginForm.password))
          )
          delete postData.captcha
          this.$store
            .dispatch('loginByUsername', postData)
            .then(res => {
              this.loading = false
              if (res.code === 0) {
                const redirectTo = _this.$route.query.redirect || '/'
                _this.$router.push({ path: redirectTo })
              } else {
                this.$message({
                  showClose: true,
                  message: '登录失败：' + res.msg,
                  type: 'error'
                })
              }
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  .login-wrap{
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    height: 100%;
    background-image: url('/static/images/login/bg.png');
    background-size: cover;
  }
  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
  }

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;
    z-index:10;
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
    .input-icon{
      height: 39px;width: 14px;margin-left: 2px;
    }
  }
  .login-code {
    width: 33%;
    display: inline-block;
    height: 38px;
    float: right;
    img{
      cursor: pointer;
      vertical-align:middle
    }
  }
  .link-a-wape {
    text-align: left;
    // font-size: 14px;
    padding: 5px;

    a{
      color: #409eff;
      text-decoration: none;
      margin-right: 15px;

      &:hover{
        color: #f40;
      }
    }

    .link-svg-weixin {
      color: #51c332;
      font-size: 18px;
      margin-bottom: -2px;
      margin-right: 2px;
    }
  }

  .singup {
    background-color: #d9534f;
    display: inline;
    float: right;
    padding: 0.2em 0.6em 0.3em;
    font-size: 75%;
    font-weight: bold;
    line-height: 1.5;
    color: #fff;
    text-align: center;
    white-space: nowrap;
    vertical-align: baseline;
    border-radius: 0.25em;
    text-decoration: none;

    &:hover{
      background-color: #c24c49;
      color:#fff;
    }
  }
  .forget-pwd {
    display: inline;
    float: right;
    padding: 0.4em 0.6em 0.3em;
    font-size: 75%;
    line-height: 1.5;
    color: #6c6c6c;
    text-align: center;
    white-space: nowrap;
    vertical-align: baseline;
    text-decoration: none;

    &:hover {
      color: #f40;
    }
  }

  .ms-tips {
    position: absolute;
    bottom:10px;
    width: 100%;
    text-align: center;
    color: #c2c3c3;
    font-size: 120%;
  }

  .ms-focus-on-wechat {
    position: absolute;
    left: 50vw;
    top: 0;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    flex-direction: column;
    margin-left: 200px;
    padding: 10px 40px;
    justify-content: center;
    align-items: center;
    height: 100%;
  }
</style>
